package com.mlog.njyjy.ndop.process.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.common.domain.Node;
import com.mlog.njyjy.ndop.process.admin.service.NodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/19
 */
@Api(value = "节点管理")
@RestController
@CrossOrigin
@RequestMapping("/pm/nodes")
public class NodeController {

    @Autowired
    private NodeService service;

    @ApiOperation(value = "列出指定节点组的所有节点", tags = "节点管理")
    @GetMapping("/listByGroup")
    public Resp listByGroup(@RequestParam Long groupId) {
        List<Node> nodeList = service.listByGroup(groupId);
        if (nodeList != null) {
            return Resp.of(nodeList);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "保存节点", tags = "节点管理")
    @PostMapping
    public Resp saveNode(@RequestBody Node node) {
        boolean success = service.save(node);
        return success ? Resp.OK : Resp.fail(500, "保存失败");
    }

    @ApiOperation(value = "删除单个节点", tags = "节点管理")
    @DeleteMapping("/{id}")
    public Resp deleteOne(@PathVariable Long id) {
        boolean success = service.delete(id);
        return success ? Resp.OK : Resp.fail(500, "删除失败");
    }

    @ApiOperation(value = "批量删除", tags = "节点管理")
    @DeleteMapping
    public Resp deleteBatch(@RequestParam String ids) {
        boolean success = service.deleteBatch(ids);
        return success ? Resp.OK : Resp.fail(500, "删除失败");
    }

    @ApiOperation(value = "导出节点配置", tags = "节点管理")
    @GetMapping("/export")
    public void exportReport(HttpServletResponse response, @RequestParam(required = false) Long groupId, @RequestParam(required = false) String fileName) {
        byte[] buffer = service.exportFile(groupId);
        // 清空response
        response.reset();
        // 设置response的Header
        try {
            if (StrUtil.isBlank(fileName)) {
                fileName = "节点配置";
            }
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
            OutputStream os = null;

            os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(buffer);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @ApiOperation(value = "导入节点文件", tags = "节点管理")
    @PostMapping("/import")
    public Resp importNodes(MultipartFile file, Long groupId) {
        try {
            boolean succeed = service.importFile(file, groupId);
            return succeed ? Resp.OK : Resp.fail(500, "解析过程发生错误...");
        } catch (IOException e) {
            e.printStackTrace();
            return Resp.fail(500, e.getLocalizedMessage());
        }
    }


    @ApiOperation(value = "更换组", tags = "节点管理")
    @PostMapping("/changeGroup")
    public Resp changeGroup(@RequestParam Long groupId, @RequestParam String ids) {
        boolean succeed = service.changeGroup(groupId, ids);
        return succeed ? Resp.OK : Resp.fail(500, "更换组过程中发生错误...");
    }

    @ApiOperation(value = "复制节点", tags = "节点管理")
    @PostMapping("/copyNode")
    public Resp copyNode(@RequestParam Long nodeId, @RequestParam String name) {
        boolean succeed = service.copyNode(nodeId, name);
        return succeed ? Resp.OK : Resp.fail(500, "复制节点过程中发生错误...");
    }

}
